package servlet;

import model.DBUtil;
import model.Puser;
import model.Ruser;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.sql.*;

@WebServlet("/rter")
public class 招人注册 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String rname = req.getParameter("rname");
        String rphone = req.getParameter("rphone");
        String rpassword = req.getParameter("rpassword");
        String remail = req.getParameter("remail");
        PrintWriter out = resp.getWriter();
        if(rname.length()==0||rphone.length()==0||rphone.length()==0||remail.length()==0){
            out.print("<script language='javascript'>alert('Please fill in the registration information correctly!!');window.location.href='/zhuce.html';</script>");
            return;
        }
        Ruser ruser;
        try {
            ruser = save(rname, rphone, rpassword,remail);

            HttpSession session = req.getSession();
            session.setAttribute("rcurrentUser", ruser);

            //完成登录
            //引导浏览器回到首页
            resp.sendRedirect("/");
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
    }

    private Ruser save(String rname, String rphone, String rpassword,String remail) throws SQLException {
        rpassword = hash(rpassword);
        try (Connection c = DBUtil.getConnection()) {
            String sql = "INSERT INTO ruser(rname,rphone,rpassword,remail) VALUES(?,?,?,?)";
            try (PreparedStatement s = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
                s.setString(1, rname);
                s.setString(2, rphone);
                s.setString(3,  rpassword);
                s.setString(4,  remail);

                s.executeUpdate();

                try (ResultSet rs = s.getGeneratedKeys()) {
                    if (!rs.next()) {
                        return null;
                    }
                    Ruser user = new Ruser();
                    user.rname = rname;
                    user.rphone = rphone;
                    user.rpassword = rpassword;
                    user.remail = remail;
                    user.ruid = rs.getInt(1);
                    return user;
                }
            }
        }
    }

    private String hash(String rpassword) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
            byte[] bytes = rpassword.getBytes("UTF-8");
            byte[] digest = messageDigest.digest(bytes);
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();

        }catch (Exception exc){
            return rpassword;
        }
    }
    }

